package com.tanhua.management.contrller;


import com.tanhua.management.pojo.Manager;
import com.tanhua.management.service.SystemService;
import com.tanhua.management.utils.Captcha;
import com.tanhua.management.utils.NoAuthorization;
import com.tanhua.management.vo.ErrorResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;


/**
 * 登录接口
 */
@RestController
@RequestMapping("management/system/users/")
public class SystemController {
    @Autowired
    private SystemService systemService;




    /**
     * 登录
     *
     * @param param
     * @param response
     * @param request
     * @return
     */
    @NoAuthorization
    @PostMapping("login")
    public ResponseEntity<Object> login(@RequestBody Map<String, String> param, HttpServletResponse response, HttpServletRequest request) {
//    public ResponseEntity<Object> login(@RequestParam("username") String username,
//                                        @RequestParam("password") String password,
//                                        @RequestParam("verificationCode") String code,
//                                        @RequestParam("uuid") String uuid,
//                                        HttpServletResponse response,
//                                        HttpServletRequest request) {
        //读取用户名 密码 验证码 图片uuid
        String username = param.get("username");
        String password = param.get("password");
        String code = param.get("verificationCode");
        String uuid = param.get("uuid");
        //获得客户端ip地址
        String xFor = request.getHeader("X-Forwarded-For");
        String[] split = xFor.split(",");
        String addr = split[0];
//        System.out.println(s);
//        System.out.println(xFor);
//        String addr = request.getRemoteAddr();

//        System.out.println(addr);


        //ErrorResult result = systemService.login(addr, username, password, uuid, code);

        ErrorResult result = null;

        //验证失败
        if (result != null) {
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(result);
        }


        //验证成功返回token
        String token = systemService.gainToken(username);
        Map<String, String> data = new HashMap<>();
        data.put("token", token);

        return ResponseEntity.ok(data);
    }

    //获取用户信息
    @PostMapping("profile")
    public Map<String, String> message(@RequestHeader("Authorization") String authorization, HttpServletRequest request,HttpServletResponse response) {
        //将token传入后端验证
        String xFor = request.getHeader("X-Forwarded-For");
        String[] split = xFor.split(",");
        String addr = split[0];

        Manager manager = this.systemService.queryMangerByToken(authorization,addr);

        if (manager == null) {
            //验证未通过
            response.setStatus(401);
            return null;

        }

        manager = this.systemService.queryManager(manager.getUid());
        Map<String, String> map = new HashMap<String, String>();
        map.put("uid", manager.getUid());
        map.put("username", manager.getUsername());
        map.put("avatar", manager.getAvatar());
        return map;
    }

    @PostMapping("logout")
    public void logout(@RequestHeader("Authorization") String token) {
        systemService.logout(token);
    }

    /**
     * 验证码
     * @param uuid
     * @param response
     * @param request
     */
    @NoAuthorization
    @GetMapping("verification")
    public void verification(@RequestParam(name = "uuid") String uuid, HttpServletResponse response, HttpServletRequest request) {

        response.setDateHeader("Expires", 0);
        response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
        response.addHeader("Cache-Control", "post-check=0, pre-check=0");
        response.setHeader("Pragma", "no-cache");


        Captcha captcha = new Captcha();
        String code = captcha.getRandcode(request, response);

        systemService.saveVerification(uuid, code);

    }
}
